﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DataAcessLayer;
using System.Data.Common;
using QuanLyShop.DTO;

namespace QuanLyShop.DAO
{
    public class ChiTietHoaDonBanHangDAO
    {

        #region 1. Load danh sách chi tiết hóa đơn bán hàng
        public  DataTable LayDanhSachDonNhapHang()
        {
            DataTable dt = null;
            try
            {
                DataProvider dp = new DataProvider();
                string sql = "select * from CHI_TIET_HOA_DON_BAN_HANG ct  where ct.TINHTRANG = 0";
                DbCommand command = dp.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = sql;
                dt = dp.ExecuteSelectCommand(command);
            }
            catch (Exception e)
            {

                throw new Exception("Lỗi rồi" + e.ToString());
            }
            return dt;

        }
        #endregion

        #region 2. Thêm chi tiết hóa đơn bán hàng

        public int ThemChiTietHoaDonBanHang(ChiTietHoaDonBanHangDTO ctdbh)
        {
            int kq = 0;
            try
            {

                DataProvider dp = new DataProvider();
                String sql = "insert into CHI_TIET_HOA_DON_BAN_HANG(ID_CHITIETHOADONBAN,ID_HOADONBAN,ID_SANPHAM,SOLUONG,TONGTIEN,THONGTINTHEM,TINHTRANG) values ('"+ctdbh.ID_ChiTietHoaDon+"','" + ctdbh.ID_HoaDonBan + "','" + ctdbh.ID_SanPham + "'," + ctdbh.SoLuong + "," +ctdbh.TongTien +",'"+ ctdbh.ThongTinThem + "'," + ctdbh.TinhTrang + ")";
                DbCommand command = dp.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = sql;
                dp.ExecuteNonQueryCommand(command);
                kq = 1;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return kq;

        }
        #endregion


        #region 3. Cập nhật chi tiết hóa đơn bán hàng

        public int CapNhatChiTietHoaDonBanHang(ChiTietHoaDonBanHangDTO ctdbh)
        {
            int kq = 0;
            try
            {

                DataProvider dp = new DataProvider();
                String sql = "update CHI_TIET_HOA_DON_BAN_HANG set ID_HOADONBAN='" + ctdbh.ID_HoaDonBan + "',ID_SANPHAM ='" + ctdbh.ID_SanPham + "',SOLUONG=" + ctdbh.SoLuong + ",TONGTIEN="+ctdbh.TongTien+",THONGTINTHEM='" + ctdbh.ThongTinThem + "',TINHTRANG=" + ctdbh.TinhTrang + " where ID_CHITIETHOADONBAN='" + ctdbh.ID_ChiTietHoaDon+"'";
                DbCommand command = dp.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = sql;
                dp.ExecuteNonQueryCommand(command);
                kq = 1;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return kq;

        }
        #endregion
        #region 4. Mã tăng tự động

        public DataTable Lay()
        {

            DataTable dt = null;
            try
            {
                DataProvider dp = new DataProvider();
                string sql = "select ID_CHITIETHOADONBAN from CHI_TIET_HOA_DON_BAN_HANG";
                DbCommand command = dp.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = sql;
                dt = dp.ExecuteSelectCommand(command);

            }
            catch (Exception e)
            {

                throw new Exception("Lỗi rồi" + e.ToString());
            }
            return dt;
        }
        public string MaTangTuDong()
        {
            string str = "";
            DataTable dt = Lay();
            PhatSinhMaTangTuDong l = new PhatSinhMaTangTuDong();
            string a = dt.Rows[dt.Rows.Count - 1].ItemArray[0].ToString();
            string b = a.Substring(0, 6);
            string c = l.LayChuoiNgayThangNam();
            if (b.Equals(c))
            {
                string p = a.Substring(6);
                int so = int.Parse(p);
                so = so + 1;
                if (so < 10)
                {
                    str = "000" + so;

                }
                else if (so < 100)
                {
                    str = "00" + so;

                }
                else if (so < 1000)
                {
                    str = "0" + so;
                }
                str = c + str;
            }
            else
            {
                str = c + "0001";
            }

            return str;
        }
        #endregion
    }
}
